package com.nantai.jiafang.nantaijiafang.zhijian.fragment;

import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import com.blankj.utilcode.util.StringUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.nantai.jiafang.nantaijiafang.R;
import com.nantai.jiafang.nantaijiafang.ViewModel.Zhijian_Net;
import com.nantai.jiafang.nantaijiafang.base.BaseFragment;
import com.nantai.jiafang.nantaijiafang.bean.ZhiJjian.MLBodyItem;
import com.nantai.jiafang.nantaijiafang.bean.ZhiJjian.ReasonRemark;
import com.nantai.jiafang.nantaijiafang.bean.ZhiJjian.Tip;
import com.nantai.jiafang.nantaijiafang.bean.ZhiJjian.ZhijianDetailAdd;
import com.nantai.jiafang.nantaijiafang.databinding.FragmentZhijianDetailAddBinding;


import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static com.android.volley.VolleyLog.TAG;

/**
 * 新增/修改【查看明细子项】
 */

public class ZhijianFragment_detail_add extends BaseFragment<FragmentZhijianDetailAddBinding> {
    private FragmentZhijianDetailAddBinding mbinding;
    private String guid,did,hid,reasonRemark;
    private MLBodyItem mlBodyItem;
    private Integer SDefectNo;// 实疵点数

    private  int reasonType;
    private static InterfaceToFragment mFragmentManager;
    private List<String> reasonMarkList=new ArrayList<>();

    private  Gson gson = new GsonBuilder().serializeNulls().create();

    private ArrayAdapter<String> reasonMarkadapter;
    public static ZhijianFragment_detail_add newinit(MLBodyItem mlBodyItem){
        ZhijianFragment_detail_add zhijianFragment_detail_add=new ZhijianFragment_detail_add();
        zhijianFragment_detail_add.setdata(mlBodyItem);
        return zhijianFragment_detail_add;
    }
    public void setdata(MLBodyItem mlBodyItem){
        this.mlBodyItem=mlBodyItem;
    }
    @Override
    public int setlayoutid() {
        return R.layout.fragment_zhijian_detail_add;
    }
    @Override
    public void initUI(FragmentZhijianDetailAddBinding binding) {
        EventBus.getDefault().register(this);
            guid="";
            did="";
        mbinding=binding;
//
        if (mlBodyItem!=null) {
            mbinding.setMLBodyItem(mlBodyItem);
            guid = mlBodyItem.getGuid();
            did = mlBodyItem.getDID();
            hid = mlBodyItem.getHID();
            reasonRemark = mlBodyItem.getResultMark();
            initspanner(mlBodyItem.getHandleTouch(),mlBodyItem.getUnpleasedSmell(),mlBodyItem.getResult(),mlBodyItem.getColouredLight(),mlBodyItem.getEdgeDiff(),mlBodyItem.getHeadTailDiff());
        }else{
            binding.UnpleasedSmell.setSelection(1,true);
        binding.ColouredLight.setSelection(6,true);
        binding.EdgeDiff.setSelection(7,true);
        binding.HeadTailDiff.setSelection(7,true);
        }

        binding.detailSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveData(false);
            }
        });
        binding.btnDefects.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!StringUtils.isEmpty(binding.ADefectNo.getText())){
                    SDefectNo=Integer.valueOf(binding.ADefectNo.getText().toString());
                    EventBus.getDefault().postSticky(SDefectNo);
                }

                if(!StringUtils.isEmpty(binding.DefectDes.getText())){
                    EventBus.getDefault().postSticky(binding.DefectDes.getText().toString());
                }

                getActivity().getSupportFragmentManager().beginTransaction()
                        .addToBackStack(null)
                        .setCustomAnimations(R.anim.in_from_left,R.anim.in_from_right,R.anim.out_from_left,R.anim.out_from_right)
                        .add(R.id.container2,new ZhijianFragment_Defects())
                        .commit();
            }
        });
        binding.detailBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().popBackStack();
            }
        });
        binding.detailAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveData(true);
            }
        });

        binding.Result.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String value=  (String)parent.getItemAtPosition(position);
                if(position == 0 || position == 1){
                    binding.detailReasonMark.setVisibility(View.VISIBLE);
                    binding.detailReasonMarkSp.setVisibility(View.GONE);
                    reasonType = 0;
                }else{
                    binding.detailReasonMark.setVisibility(View.GONE);
                    binding.detailReasonMarkSp.setVisibility(View.VISIBLE);

                    reasonType = 1;

                    getReasonRemark(value,reasonRemark);
                }
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        binding.detailReasonMarkSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
               reasonRemark =  (String)parent.getItemAtPosition(position);

            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }
    public void setdefectDes(String context){
        mbinding.DefectDes.setText(context);
    }

    public void setADefectNo(String count){
        mbinding.ADefectNo.setText(count);
    }
    private void initspanner(String touch,String UnpleasedSmell,String result,String ColouredLight,String EdgeDiff,String HeadTailDiff){
        switch (touch){
            case "好":
                mbinding.HandleTouch.setSelection(0);
                break;
            case "差":
                mbinding.HandleTouch.setSelection(1);
                break;
        }
        switch (UnpleasedSmell){
            case "有":
                mbinding.UnpleasedSmell.setSelection(0);
                break;
            case "无":
                mbinding.UnpleasedSmell.setSelection(1);
                break;
        }
        switch (result){
            case "合格":
                mbinding.Result.setSelection(1);
                mbinding.detailReasonMark.setVisibility(View.VISIBLE);
                mbinding.detailReasonMarkSp.setVisibility(View.GONE);
                reasonType = 0;
                break;
            case "不合格":
                mbinding.Result.setSelection(2);
                mbinding.detailReasonMark.setVisibility(View.GONE);
                mbinding.detailReasonMarkSp.setVisibility(View.VISIBLE);
                reasonType = 1;
                break;
            case "二等品":
                mbinding.Result.setSelection(3);
                mbinding.detailReasonMark.setVisibility(View.GONE);
                mbinding.detailReasonMarkSp.setVisibility(View.VISIBLE);
                reasonType = 1;
                break;
            default:
                mbinding.Result.setSelection(0);
                mbinding.detailReasonMark.setVisibility(View.VISIBLE);
                mbinding.detailReasonMarkSp.setVisibility(View.GONE);
                reasonType = 0;
                break;
        }

        switch (ColouredLight){
            case "1":
                mbinding.ColouredLight.setSelection(0);
                break;
            case "1-2":
                mbinding.ColouredLight.setSelection(1);
                break;
            case "2":
                mbinding.ColouredLight.setSelection(2);
                break;
            case "2-3":
                mbinding.ColouredLight.setSelection(3);
                break;
            case "3":
                mbinding.ColouredLight.setSelection(4);
                break;
            case "3-4":
                mbinding.ColouredLight.setSelection(5);
                break;
            case "4":
                mbinding.ColouredLight.setSelection(6);
                break;
            case "4-5":
                mbinding.ColouredLight.setSelection(7);
                break;
            case "5":
                mbinding.ColouredLight.setSelection(8);
                break;
        }

        switch (EdgeDiff){
            case "1":
                mbinding.EdgeDiff.setSelection(0);
                break;
            case "1-2":
                mbinding.EdgeDiff.setSelection(1);
                break;
            case "2":
                mbinding.EdgeDiff.setSelection(2);
                break;
            case "2-3":
                mbinding.EdgeDiff.setSelection(3);
                break;
            case "3":
                mbinding.EdgeDiff.setSelection(4);
                break;
            case "3-4":
                mbinding.EdgeDiff.setSelection(5);
                break;
            case "4":
                mbinding.EdgeDiff.setSelection(6);
                break;
            case "4-5":
                mbinding.EdgeDiff.setSelection(7);
                break;
            case "5":
                mbinding.EdgeDiff.setSelection(8);
                break;
        }

        switch (HeadTailDiff){
            case "1":
                mbinding.HeadTailDiff.setSelection(0);
                break;
            case "1-2":
                mbinding.HeadTailDiff.setSelection(1);
                break;
            case "2":
                mbinding.HeadTailDiff.setSelection(2);
                break;
            case "2-3":
                mbinding.HeadTailDiff.setSelection(3);
                break;
            case "3":
                mbinding.HeadTailDiff.setSelection(4);
                break;
            case "3-4":
                mbinding.HeadTailDiff.setSelection(5);
                break;
            case "4":
                mbinding.HeadTailDiff.setSelection(6);
                break;
            case "4-5":
                mbinding.HeadTailDiff.setSelection(7);
                break;
            case "5":
                mbinding.HeadTailDiff.setSelection(8);
                break;
        }
    }
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void setHid(String hid){
        this.hid=hid;
        EventBus.getDefault().unregister(this);
    }


    private void getReasonRemark(String value,String reason){
        reasonMarkList.clear();
        Map<String,String> maps=new HashMap<>();
        maps.put("Result",value);
        Zhijian_Net.getReasonRemark(getContext(), maps,new Zhijian_Net.GetSingData<List<ReasonRemark>>() {
            @Override
            public void success(List<ReasonRemark> s) {
                for (ReasonRemark reasonRemark : s) {
                    reasonMarkList.add(reasonRemark.getValue());
                }

                int pos = reasonMarkList.indexOf(reason);

                reasonMarkadapter=new ArrayAdapter<String>(getContext(),android.R.layout.simple_spinner_dropdown_item,reasonMarkList);
                mbinding.detailReasonMarkSp.setAdapter(reasonMarkadapter);

                mbinding.detailReasonMarkSp.setSelection(pos);
                reasonMarkadapter.notifyDataSetChanged();
            }
        });
    }

    private void saveData(boolean isclean){
        Map<String,String> maps=new HashMap<>();

        maps.put("opType","saveEInspectDetail");
        maps.put("Guid",guid);
        maps.put("DID",did);
        maps.put("HID",hid);
        maps.put("BatchReelNo",mbinding.BatchReelNo.getText().toString());
        maps.put("SMeters",mbinding.SMeters.getText().toString());
        maps.put("MeasuredMeters",mbinding.MeasuredMeters.getText().toString());
        maps.put("SDefectNo",mbinding.SDefectNo.getText().toString());
        maps.put("ADefectNo",mbinding.ADefectNo.getText().toString());

        maps.put("DefectDes",mbinding.DefectDes.getText().toString());
        maps.put("MeasuredWidth",mbinding.MeasuredWidth.getText().toString());
        maps.put("HandleTouch",mbinding.HandleTouch.getSelectedItem().toString());
        maps.put("UnpleasedSmell",mbinding.UnpleasedSmell.getSelectedItem().toString());
        maps.put("BowWeft",mbinding.BowWeft.getText().toString());
        maps.put("BowWidth",mbinding.BowWidth.getText().toString());
        maps.put("ColouredLight",mbinding.ColouredLight.getSelectedItem().toString());
        maps.put("EdgeDiff",mbinding.EdgeDiff.getSelectedItem().toString());
        maps.put("HeadTailDiff",mbinding.HeadTailDiff.getSelectedItem().toString());
        maps.put("Result",mbinding.Result.getSelectedItem().toString());
        maps.put("Remark",mbinding.Remark.getText().toString());
      maps.put("SeqNo",mbinding.SeqNo.getText().toString());
//        maps.put("EInspect",mbinding.EInspectNo.getText().toString());
        maps.put("ResultMark",reasonType == 0?mbinding.detailReasonMark.getText().toString():reasonRemark);

        for (Iterator<Map.Entry<String, String>> it = maps.entrySet().iterator(); it.hasNext();){
            Map.Entry<String, String> item = it.next();

            if(StringUtils.isEmpty(item.getValue())){
                it.remove();
            }

        }


        //新增/修改表体信息
        Zhijian_Net.saveEInspectHead(getContext(), maps, new Zhijian_Net.GetSingData<List<Tip>>() {
            @Override
            public void success(List<Tip> s) {
                Log.e(TAG, "success: "+s.get(0).getTip() );
                toast(s.get(0).getTip());
                if (isclean) {
                    mbinding.setMLBodyItem(new MLBodyItem());
                }else{
                    //getFragmentManager().beginTransaction()
                    //ZhijianFragment_detail_add.super.onStart();
                    //ZhijianFragment_detail fragment=(ZhijianFragment_detail)getActivity().getSupportFragmentManager().findFragmentByTag("ZhijianFragment_detail");
                    //fragment.setPosition(hid);
                    //fragment.getdetaile();
                    getFragmentManager().popBackStack();

                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
